#!./perl

# ------------------------------------------------------------

# addshow.pl, by Jean-Pierre Girard (jpg@corrado.sun.com)
# This helps the user to insert a slide-show or a demo of
# its product in the cdware

# ------------------------------------------------------------

# Bugs and other fixes

# ------------------------------------------------------------
%DefaultShellnames = (
"DEMO", "install_show.sh",
"TESTDRIVE", "run.sh",
"INSTALL", "install.sh",
);

%DefaultFilenames = (
"DEMO", "show",
"TESTDRIVE", "run",
"INSTALL", "install",
);

%WindowTitle = (
"DEMO", "Run Presentation",
"TESTDRIVE", "Run Trial Software",
"INSTALL", "Install Trial Software",
);
#############################################################################
# CreateFiles: create the shell scripts
#############################################################################
sub CreateFiles
{
	local($old);
	#print "bef open: request = $request<br>";
	%rqpairs = split(/[&=]/, $request);
	$request =~ s/^(TESTDRIVE|DEMO|INSTALL).*/\1/;
	$ShellFile = "$InstallDir/$DefaultShellnames{$request}";
	$ScriptFile = "$InstallDir/$rqpairs{$request}";
	#print "bef open: request = $request.<br>";
	if (open(SHELL_FILE, ">$ShellFile") || !&PrintErr("NoCreation", $ShellFile)) {
		write SHELL_FILE;

		close (SHELL_FILE);
		chmod (0777, $ShellFile);
		if ($rqpairs{"CreateDefault"} eq "yes" ) {
			if (open(SCRIPT_FILE, ">$ScriptFile") || !&PrintErr("NoCreation", $ScriptFile)) {
				#print "after open $ScriptFile<br>";
				$old = select(SCRIPT_FILE);
				#print STDOUT "bef if<br>";
				if ($request eq "TESTDRIVE") {
					#print STDOUT "after testdr $ScriptFile<br>";
					$~ = "DEFAULT_SHOW_SCRIPT";
				} elsif ($request eq "INSTALL") {
					#print STDOUT "after install $ScriptFile<br>";
					$~ = "DEFAULT_INST_SCRIPT";
				} elsif ($request eq "DEMO") {
					#print STDOUT "after demo $ScriptFile<br>";
					$~ = "DEFAULT_RUN_SCRIPT";
				}
				#print STDOUT "bef if<br>";
				write SCRIPT_FILE;
				#print STDOUT "bef close<br>";
				close(SCRIPT_FILE);
				chmod (0777, $ScriptFile);
				#print STDOUT "bef sel<br>";
				select($old);
				#print STDOUT "after sel<br>";
				#system("textedit $ScriptFile&");
				#print "bef textedit $ScriptFile, <br>BASEDIR=$BASEDIR<br>";
				#print `/usr/openwin/bin/textedit $ScriptFile`;
				#print `/usr/openwin/bin/textedit $ScriptFile&`;
				#print `pwd; echo "<br>"; hostname;echo "<br>";xhost +; id`;
				#$HostName = `hostname`;
				#print `(xhost +$HostName;$BASEDIR/DEV/bin/textedit.sh $ScriptFile) >/home/jpg/.products/.bin/httpd/Solaris_2/jpg4`;
				#print `$BASEDIR/DEV/bin/textedit.sh $ScriptFile >/home/jpg/.products/.bin/httpd/Solaris_2/jpg4`;
			}
		}
		$request = "";
	}
}

#############################################################################
# AskFileName: Ask for the name of the script
#############################################################################
sub OffersEditScript
{
	#print "In Offer=$_[0]<br>";
	$ScriptName = "$InstallDir/$DefaultShellnames{$_[0]}";
	#print "ScriptName=$ScriptName.<br>";
	if (-s $ScriptName && open (SCRIPTFILE, $ScriptName)) {
		$CustName = "";
		while (<SCRIPTFILE>) {
			#print "$_<br>";
			if ($_ =~ /\/_install\//) {
				#print "found=$_<br>";
				$CustName = $_;
				$CustName =~ s#.*/([^/ ]*) *\n$#\1#;
				last;
			}
		}
		#$CustName = "$InstallDir/$CustName";
		#print "CustName=$CustName.<br>";
		if (-s "$InstallDir/$CustName") {
			print "<ul>";
			print "<IMG src=\"http://localhost:5759/DEV/images/bullet_ball_green.gif\"><a href=\"http://localhost:5759/cgi-bin/addshow.pl?textedit=$InstallDir/$CustName\">To edit the lauching script of your product</a><br>";
			$ScriptName =~ s?.*/([^/]*/_install/[^/]*)$?\1?;
			print "<IMG src=\"http://localhost:5759/DEV/images/bullet_ball_green.gif\"><a href=\"http://localhost:5759/$ScriptName\">To test your script</a><br>";
			print "</ul>";
		} else {
			print "<ul><h4><applet class=\"ImageLoopItem\" img=\"http://localhost:5759/DEV/images/blinkingAlert\" ALIGN=middle pause=0 width=56 height=49 pause = 200>You need to create a script called \"$CustName\"</h4></ul>";
			#print "<ul><h4><applet class=\"ImageLoopItem\" img=\"http://localhost:5759/DEV/images/blinkingAlert\" ALIGN=middle pause=0 width=56 height=49 pause = 200>You need to create a script called \"$CustName\"</h4></ul>";
		}
	} else {
		print "<ul><h4><applet class=\"ImageLoopItem\" img=\"http://localhost:5759/DEV/images/blinkingAlert\" ALIGN=middle pause=0 width=56 height=49 pause = 200>You need to select the previous link to create your scripts</h4></ul>";
		#print "<ul><h4><IMG ALIGN=middle SRC=\"http://localhost:5759/DEV/images/finger.gif\">You need to select the previous link to create your scripts</h4></ul>";
	}
}

#############################################################################
# AskFileName: Ask for the name of the script
#############################################################################
sub AskFileName
{
	print "<h2><IMG ALIGN=middle SRC=\"http://localhost:5759/DEV/images/finger.gif\" >Read this:</h2><p>You need to specify the name of your script that will do the job. If you don't already have one, select \"Create a default script\". This will create a default script that you can edit later to fit your needs.<br>"
		,"<p>Enter the filename of your script:"
		, "<FORM method=\"post\"  ACTION=\"http://localhost:5759/cgi-bin/addshow.pl?$_[0]\"> <INPUT TYPE=\"text\" value=\""
		, $DefaultFilenames{$_[0]}
		, "\" NAME=\"$_[0]\" SIZE=\"20\" MAXLENGTH=50></p>"
		, "<p><input type=\"checkbox\" NAME=\"CreateDefault\" VALUE=\"yes\">Create a default script <br>"
		, "<center><input type=\"submit\" VALUE=\"Create File\" ></center></FORM>";
	$~ = FOOTER;
	write;
}

#############################################################################
# main
#############################################################################
$BASEDIR = $ENV{"DOCUMENT_ROOT"};
require 'devinfo.pl';
require 'tools.pl';

$HomeDir = &GetDevinfo("dir");
$ShortHomeDir = $HomeDir;
$ShortHomeDir =~ s?.*/([^/]*)$?\1?;

$InstallDir = "$HomeDir/_install";
$INFOFILENAME = $HomeDir . "/_info";

if (-s $INFOFILENAME && open (INFOFILE, $INFOFILENAME)) {
	$LINE=<INFOFILE>;
	($Company, $Description, $Platform) = split(/\^/, $LINE);
	close (INFOFILE);
}
if ($ENV{'REQUEST_METHOD'} eq "POST") {
	read(STDIN, $request, $ENV{'CONTENT_LENGTH'});
} elsif ($ENV{'REQUEST_METHOD'} eq "GET" ) {
	$request = $ENV{'QUERY_STRING'};
}

$~ = HEADER;
write;

#system ("$BASEDIR/DEV/bin/textedit.sh \"TEST\" >/home/jpg/DEV/.products/.bin/httpd/Solaris_2/jpg4");
#print "ShortHomeDir = $ShortHomeDir<br>";
#print "request=$request<br>";
$request =~ s/%(..)/pack("c",hex($1))/ge;
#print "request=$request<br>";
#print "ARGV0=$ARGV[0]<br>";

#if (@ARGV == 0 && $request eq "") {

#	#print "in case argv=0";
#} elsif ($request eq "default=Create") {
#	#print "bef CreateDefault<br>";
#} else {
#}

if ($request ne "") {
	#print "not null request=$request.<br>";
	unless (-d $InstallDir) {
		mkdir($InstallDir, 0777) || &PrintErr("NoDirCreate", $InstallDir);
	}
}
#if ($request =~ /^TESTDRIVE$/) {
if ($request =~ /^(TESTDRIVE|DEMO|INSTALL)=/) {
	#print "request TESTDRIVE=$request.<br>";
	&CreateFiles($request);
} elsif ($request =~ /^(TESTDRIVE|DEMO|INSTALL)$/) {
	#print "request TESTDRIVE=$request.<br>";
	&AskFileName($request);
#elsif ($request eq "DEMO") {
#elsif ($request eq "INSTALL") {
}

if ($request =~ /textedit=/ ) {
	$ScriptName = $request;
	$ScriptName =~ s/textedit=//;
	#unless (fork) {
		#exec ("textedit $ScriptName");
	#}
	system ("textedit $ScriptName&");
	$request = "";
}

if ($request eq "") {
		print "<IMG src=\"http://localhost:5759/DEV/images/finger.gif\">Please copy all the necessary files under the \"$HomeDir/_install\" directory<br>";
	if ($Platform =~ /demo/ ) {
		print "<h2><IMG src=\"http://localhost:5759/DEV/images/bullet_ball_red.gif\"><a href=\"http://localhost:5759/cgi-bin/addshow.pl?DEMO\">To include a slide-show about your product</a></h2><br>";
		&OffersEditScript("DEMO");
	}
	if ($Platform =~ /testdrive/ ) {
		print "<h2><IMG src=\"http://localhost:5759/DEV/images/bullet_ball_red.gif\"><a href=\"http://localhost:5759/cgi-bin/addshow.pl?TESTDRIVE\">To include a demo of your product</a></h2><br>";
		&OffersEditScript("TESTDRIVE");
	$~ = FOOTER;
	}
	if ($Platform =~ /install/ ) {
		print "<h2><IMG src=\"http://localhost:5759/DEV/images/bullet_ball_red.gif\"><a href=\"http://localhost:5759/cgi-bin/addshow.pl?INSTALL\">To include an unlockable or light version of your product</a></h2><br>";
		&OffersEditScript("INSTALL");
	}
	print "<h2><IMG src=\"http://localhost:5759/DEV/images/bullet_ball_red.gif\"><a href=\"http://localhost:5759/cgi-bin/mainmenu.pl?\">To go back to the main menu</a></h2><br>";

	select(STDOUT);
	$~ = FOOTER;
	write;
}

#############################################################################
# formats
#############################################################################
format HEADER =
Content-type: text/html


<html>
<title>Developer's Kit</title>
<body>
<h1>CDware Developer's Kit</h1>
<h4>Currently working with "@*
$HomeDir
"</h4>
<ul type=1>
.

format FOOTER =
</ul>
<p>
<hr>
#<p>
#You, like these companies, will benefit from the CDware capability of reaching tens of thousands of potential customers with information, links to your web site, or directly with YOUR product!
#<p><applet class=ImageTape width=596 height=65 img_dir=../../../DEV/logos speed=10 dir=-10
#url1=oracle
#url2=progress
#url3=gupta
#url4=informix
#url5=lotus
#url6=ingres
#>
</b>
<h5><p><A HREF=http://localhost:5759/CDware/Help/SMICopyright.html>Copyright</A> 1995 Sun Microsystems, Inc., 2550 Garcia Ave., Mtn. View, CA 94043 USA.  All rights reserved.</h5><br></pre>
<a href=http://localhost:5759/DEV/credits.html>Credits</a>
</b>
</form>
</body>
</html>
.

format SHELL_FILE =
 #! /bin/sh
 #
 #       get installation directory
 #

# currently company dir is manually placed-should change

COMPANY_DIR=$PRODUCT_DIR/@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$ShortHomeDir
export COMPANY_DIR

 #
 # execute installation script
 #

$OPENWINHOME/bin/cmdtool -Wb 250 250 210 -Wl "@<<<<<<<<<<<<<<<<<<<<<" -Wx large -Ww 78 -Wh 20 -Wp 210 210 $COMPANY_DIR/_install/@*
$WindowTitle{$request}, $rqpairs{$request}

read answer
exit 0
.

format INSTALL_INFO =
<FORM method="post"  ACTION="http://localhost:5759/cgi-bin/addshow.pl?@*
$request
"> <INPUT TYPE="text" 
~@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$Dir
 NAME="IsIndex" SIZE="35" MAXLENGTH=50>
<input type="submit" VALUE="Create File" >
</FORM>
.

format DEFAULT_INST_SCRIPT =
 #! /bin/sh  
 #
 #	Installation shell script for Catalyst CDware
 #
 # 	Usage: install_unbundled -m<mountpoint> -p<productpath>
 #
 #	Created: 	June 12, 1991
 #	Last Revised: 	May 23, 1995
 #
 #	Use this installation script to install your product from Catalyst CDware
 #
 #
 #	For technical support, please call.
 #		+1 415 336.3742 (CDware SPARC) or +1 415 336.3941 (CDware x86)
 #
 #	get command line parameters
 #

 # function for error exiting

end_it () {
	echo " "
	echo "Hit return to end ... "
	read answer
	exit 0
}

 ###############################################################################
 ###############################################################################
 #	set PRODUCT_SIZE to the size of your product in kbytes 	
 ###############################################################################
PRODUCT_SIZE=8000
 ###############################################################################
 #
 #	get complete paths
 #
 #
 #	get location of installation directory 
 ##
echo
echo "Please enter the absolute path name of the directory structure in which"
echo "you would like to install the demonstration and evaluation programs."
echo
echo "Installation directory: "
read INSTALL_DIR

 #
 #	verify the installation directory exists and is a directory
 #
if [ ! -d "$INSTALL_DIR" ]
then
	echo "Creating $INSTALL_DIR ..."
	mkdir $INSTALL_DIR
	if [ ! -d "$INSTALL_DIR" ]
	then
		echo
		echo
		echo "Could not create the installation directory."
		echo
		echo "Exiting the installation script."
                end_it
	fi
fi

 #
 #	verify write permission for the installation directory
 #
if [ ! -w "$INSTALL_DIR" ]
then
	echo
	echo
	echo "You do not have write permission for the installation directory: "
	echo $INSTALL_DIR
	echo
		echo
	echo "Exiting the installation script."
        end_it
fi

 #
 #	verify free disk space
 #
echo	
echo	"Verifying free disk space..."
FREE_SPACE=0
df=/tmp/FREE.$$
 #
 # WARNING:
 # --------
 # For Solaris 2.4, the df line below will need to be updated because
 # the standard output of the df command has changed in Solaris 2.4
 #
/usr/bin/df -k $INSTALL_DIR | sed /kbytes/d | sed -e '3,$d' | sed -e '1,$s/  */ /g' > $df

case `wc -l $df | sed -e 's/@ *//' | cut -f1 -d" " ` in
"^"
	1) FREE_SPACE=`cat $df | cut -f4 -d" "` ;; 
	2) FREE_SPACE=`cat $df | sed 1d | sed 's/@ *//' | cut -f3 -d" "` ;; 
"^"
esac

if [ -n $FREE_SPACE ]
then
	if [ $FREE_SPACE -lt $PRODUCT_SIZE ]
	then
		echo
		echo
		echo "The product installation requires $PRODUCT_SIZE kbytes of"
		echo "free disk space. There is only $FREE_SPACE kbytes of free" 
		echo "disk space in the selected filesystem.  Please free up some"
		echo "disk space and try this installation again."
		echo
		echo "Exiting the installation script."
	        end_it 	
	fi
fi
/bin/rm $df 2>/dev/null

 #
 #	copy binaries to installation directory
 #
echo 
echo "Copying files to "$INSTALL_DIR"..."
echo
 #
 ###############################################################################
 ###############################################################################
 # uncomment the following line to do the actual copy
 # this assumes that your files are tared and compressed
 ###############################################################################

 #if [ $CDWARE_ENV != "Solaris_2" ]; then

 #	cd $INSTALL_DIR
 #	zcat $COMPANY_DIR/_install/tarfileS2.Z | tar xf -
 #else
 #	if [ $CDWARE_ENV != "Solaris_1" ]; then
 #		cd $INSTALL_DIR
 #		zcat $COMPANY_DIR/_install/tarfileS2.Z | tar xf -
 #	else
 #		if [ $CDWARE_ENV != "Solaris_x86" ]; then
 #			cd $INSTALL_DIR
 #			zcat $COMPANY_DIR/_install/tarfileS2.Z | tar xf -
 #		else
 #			echo "This product doesn't run on this platform";
 #		fi
 #	fi
 #fi
 ###############################################################################

echo
echo
echo "To run our product, do the following: "
echo
echo "1. Change directories to "$INSTALL_DIR
echo "2. Run the product by typing ./name_of_file"
echo
echo		


end_it
.

format DEFAULT_SHOW_SCRIPT=
 #!/bin/sh
 #       Installation shell script for Catalyst CDware
 #
 #       Usage: install_unbundled -m<mountpoint> -p<productpath>
 #
 #       Created:        June 12, 1991
 #       Last Revised:   Dec 22, 1995
 #
 #       Use this installation script to install your product from Catalyst CDware
 #
 #
 #       For technical support, please call.
 #               +1 415 336.3742 (CDware SPARC) or +1 415 336.3941 (CDware x86)
 #
 #       get command line parameters
 #
echo ''
echo '          Starting Slide Show...'
LD_LIBRARY_PATH=$OPENWINHOME/lib
export LD_LIBRARY_PATH

 ###############################################################################
 ###############################################################################
 # change these lines to start your slide-show
 ###############################################################################

cd $COMPANY_DIR/_install/rolling/color
/tmp/httpd/.cgi-bin/screendemo ./screendemo.menu

 ###############################################################################
echo 'Press <Return> to close the window... '
read answer
exit 0
.

format DEFAULT_RUN_SCRIPT =
 #! /bin/sh 
 #
 #	Installation shell script for Catalyst CDware
 #
 # 	Usage: install_unbundled -m<mountpoint> -p<productpath>
 #
 #	Created: 	June 12, 1991
 #	Last Revised: 	May 23, 1994
 #
 #	Use this installation script to install your product from Catalyst CDware
 #
 #
 #	For technical support, please call.
 #		+1 415 336.3742 (CDware SPARC) or +1 415 336.3941 (CDware x86)
 #
 #	get command line parameters
 #
echo ' '
 #if [ $CDWARE_ENV != "Solaris_2" ]; then
 #	echo 'Starting XXX for Solaris 2.4'
 #	run product for Solaris 2
 #else
 #	if [ $CDWARE_ENV != "Solaris_1" ]; then
 #		run product for SunOs4
 #	else
 #		if [ $CDWARE_ENV != "Solaris_x86" ]; then
 #			run product for Solaris x86
 #		else
 #			echo "This product doesn't run on this platform";
 #		fi
 #	fi
 #fi
echo ' '
echo 'Press <Return> to close the window... '
read answer
exit 0;
.
